package jinyilw.swing.visuals.Icomp;

import jinyilw.swing.i18n.IString;
import jinyilw.swing.i18n.LanguageTools;
import jinyilw.swing.visuals.Rcomp.RFileChooser;

import javax.swing.JFileChooser;
import java.awt.Component;
import java.io.File;

public class IFileChooser
{
	private RFileChooser rFileChooser;
	private String languageTag;
	private IString i18nTitle;
	private File folder;
	private String title;
	private String[] exts;
	private int mode = JFileChooser.FILES_ONLY;

	public IFileChooser()
	{
	}

	public IFileChooser(File path)
	{
		this.folder = path;
	}

	public IFileChooser(File path, String ext)
	{
		this.folder = path;
		exts = new String[1];
		exts[0] = ext;
	}

	public IFileChooser(File path, String ext, String title)
	{
		this.folder = path;
		exts = new String[1];
		exts[0] = ext;
		this.title = title;
	}

	public IFileChooser(File path, String[] exts, String title)
	{
		this.folder = path;
		this.exts = exts;
		this.title = title;
	}

	public IFileChooser(File path, String ext, IString i18nTitle)
	{
		this.folder = path;
		exts = new String[1];
		exts[0] = ext;
		this.i18nTitle = i18nTitle;
	}

	public IFileChooser(File path, String[] exts, IString i18nTitle)
	{
		this.folder = path;
		this.exts = exts;
		this.i18nTitle = i18nTitle;
	}

	public IFileChooser(IString i18nTitle)
	{
		this.i18nTitle = i18nTitle;
	}

	public IFileChooser(File path, IString i18nTitle)
	{
		this.folder = path;
		this.i18nTitle = i18nTitle;
	}

	public void setI18nTitle(IString i18nTitle)
	{
		this.i18nTitle = i18nTitle;
		if (rFileChooser != null && i18nTitle != null)
			rFileChooser.setDialogTitle(i18nTitle.getI18nString());
	}

	private void createChooser()
	{
		if (rFileChooser == null || languageTag != LanguageTools.languageTag)
		{
			if (rFileChooser != null)
				folder = rFileChooser.getCurrentDirectory();
			if (i18nTitle != null)
				title = i18nTitle.getI18nString();
			rFileChooser = new RFileChooser(folder, exts, title);
			rFileChooser.setFileSelectionMode(mode);
			languageTag = LanguageTools.languageTag;
			// System.out.println("createChooser");
		}
	}

	public void setFileSelectionMode(int mode)
	{
		this.mode = mode;
		if (rFileChooser != null)
			rFileChooser.setFileSelectionMode(mode);
	}

	public File getOpenDirectory(Component com)
	{
		createChooser();
		return rFileChooser.getOpenDirectory(com);
	}

	public File getSaveDirectory(Component com)
	{
		createChooser();
		return rFileChooser.getSaveDirectory(com);
	}

	public String getSaveDirectoryPath(Component com)
	{
		File directory = getSaveDirectory(com);
		if (directory != null)
			return directory.getAbsolutePath();
		return null;
	}

	public File openAndGetOpenFile(Component com)
	{
		createChooser();
		return rFileChooser.openAndGetOpenFile(com);
	}

	public String openAndGetOpenFilePath(Component com)
	{
		File file = openAndGetOpenFile(com);
		if (file != null)
			return file.getAbsolutePath();
		return null;
	}

	public File openAndGetSaveFile(Component com)
	{
		createChooser();
		return rFileChooser.openAndGetSaveFile(com);
	}

	public String openAndGetSaveFilePath(Component com)
	{
		File file = openAndGetSaveFile(com);
		if (file != null)
			return file.getAbsolutePath();
		return null;
	}
}
